﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Digitalmes.Sugar;
using Digitalmes.Common.Cache;
using Digitalmes.Common.Utils;
using Yitter.IdGenerator;

namespace Digitalmes.Application.IntegrationTests.Common;

public abstract class BaseService
{
    public IServiceProvider Services { get; private set; }

    protected BaseService()
    {
        var hostBuilder = Host.CreateDefaultBuilder()
           .ConfigureAppConfiguration((buildContext, appConfigBuilder) =>
           {
               appConfigBuilder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false);
           })
           .ConfigureServices((buildContext, services) =>
           {
               services.AddApplication(buildContext.Configuration);
               services.AddMemoryCache();
               services.AddScoped<ICacheService, MemoryService>();
               services.AddSqlSugarSetup(buildContext);

               ConfigureServices(services, buildContext);
           });

        var host = hostBuilder.Build();
        host.Start();

        Services = host.Services;
        AppUtils.AppSetup2(Services);

        // 雪花Id
        YitIdHelper.SetIdGenerator(new IdGeneratorOptions
        {
            WorkerId = 1, // 机器码 全局唯一
            WorkerIdBitLength = 1, // 机器码位长 默认值6，取值范围 [1, 19]
            SeqBitLength = 6, // 序列数位长 默认值6，取值范围 [3, 21]（建议不小于4，值越大性能越高、Id位数也更长）
        });
    }

    protected virtual void ConfigureServices(IServiceCollection services, HostBuilderContext context)
    {

    }
}
